﻿using Kingdee.BOS.Util;
using Kingdee.BOS.WebApi.Client;
using Startup.YunFei.Constants;
using Startup.YunFei.Models.Grasses;
using System;
using System.Collections.Generic;

namespace Startup.YunFei
{
    internal class DataImporter
    {
        private string addYunShuLuXian(K3CloudApiClient apiClient)
        {
            Func<string, int> getPriceTypeValue = priceType =>
            {
                switch (priceType)
                {
                    default:
                    case "重量计":
                        return 1;
                    case "体积计":
                        return 2;
                    case "集装箱":
                        return 3;
                    case "车次计":
                        return 4;
                }
            };

            var dataJson = new RouteJSON().Value;
            var dataCollection = JsonUtil.DeserializeObject<List<annznb112>>(dataJson);
            var now = DateTime.Now;
            var strNow = now.ToString("yyyy-MM-dd");

            dataCollection.ForEach(data =>
            {
                if (string.IsNullOrWhiteSpace(data.运输路线编码))
                {
                    return;
                }

                var model = new
                {
                    FID = 0,
                    FNumber = data.运输路线编码,
                    FName = data.运输路线名称,
                    FDescription = "",
                    F_RDLV_start_point = new { FNUMBER = data.起运点编码 },
                    F_RDLV_Destion = new { FNUMBER = data.目的地编码 },
                    F_RDLV_Carrier = new { FNAME = data.承运商名称 },
                    F_RDLV_LoadType = new { FNUMBER = "HC" },
                    F_RDLV_PriceType = getPriceTypeValue(data.计价方式),
                    F_RDLV_ShipDays = data.在途时间,
                    F_RDLV_SupplySeq = "A"
                };

                var args = new
                {
                    Model = model
                };
                const string formId = "RDLV_TransfRute";
                var json = JsonUtil.Serialize(args);
                var response = apiClient.Save(formId, json);
            });

            return null;
        }

        public void Import()
        {
            const string host = "http://10.10.11.100", dbId = "6642ecc879eb25", account = "administrator", password = "kingdee123.";

            var apiClient = new K3CloudApiClient($"{host}/k3cloud/");
            var loginResult = apiClient.ValidateLogin(dbId, account, password, 2052);

            addYunShuLuXian(apiClient);
        }
    }
}
